﻿using System;
using System.Text;
using SkaldRogueLike.Main.CellContent;
using SkaldRogueLike.Main.Common;
using SkaldRogueLike.Main.Constants.Enumerations;
namespace CatSkaldRoguelike.ConsoleGame
{
    class Program
    {
        private static GameProcessor _gameProcessor;
        private static string _text;

        static void Main(string[] args)
        {
            _gameProcessor = new GameProcessor();
            _gameProcessor.UpdateMap();
            while (!_gameProcessor.End)
            {
                DrawMap(_gameProcessor.Map);
                Console.WriteLine(_text);
                _text = MoveChar(Console.ReadKey(true));
                _text += _gameProcessor.Update();
            }
            Console.WriteLine(Messages.Die);
        }

        #region Old

        private static void DrawMap(CellContent[,] map)
        {
            Console.Clear();
            for (var x = 0; x < map.GetLength(0); x++)
            {
                for (var y = 0; y < map.GetLength(1); y++)
                {
                    Console.ForegroundColor = SetColor(map[x, y].Color);
                    Console.Write(map[x, y].Image);
                }
                Console.WriteLine();
            }
        }

        private static string MoveChar(ConsoleKeyInfo command)
        {
            var stringBuilder = new StringBuilder();
            ConsoleKeyInfo key;
            switch (command.Key)
            {
                    #region move

                case ConsoleKey.UpArrow:
                case ConsoleKey.NumPad8:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.North));
                    break;
                case ConsoleKey.DownArrow:
                case ConsoleKey.NumPad2:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.South));
                    break;
                case ConsoleKey.LeftArrow:
                case ConsoleKey.NumPad4:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.West));
                    break;
                case ConsoleKey.RightArrow:
                case ConsoleKey.NumPad6:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.East));
                    break;
                case ConsoleKey.NumPad7:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.NorthWest));
                    break;
                case ConsoleKey.NumPad9:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.NorthEast));
                    break;
                case ConsoleKey.NumPad1:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.SouthWest));
                    break;
                case ConsoleKey.NumPad3:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.SouthEast));
                    break;
                case ConsoleKey.NumPad5:
                    stringBuilder.Append(_gameProcessor.MoveCharacter(Direction.None));
                    break;

                    #endregion

                    // commands with choices
                case ConsoleKey.I:
                    Console.WriteLine(Messages.HavePattern, _gameProcessor.ShowInventory());
                    Console.WriteLine(Messages.PressToPattern, "w", "equip");
                    Console.WriteLine(Messages.PressToPattern, "d", "drop");
                    key = Console.ReadKey(true);
                    if (IsEscOrNonLetterKey(key.Key))
                        break;
                    if (key.Key == ConsoleKey.W)
                        goto case ConsoleKey.W;
                    if (key.Key == ConsoleKey.D)
                        goto case ConsoleKey.D;
                    //todo
                    //stringBuilder.AppendFormat(Messages.PressToPattern, "letter", "use");
                    break;
                case ConsoleKey.W:
                    Console.WriteLine(Messages.HavePattern + Messages.MakeYourChoice,
                                      _gameProcessor.ShowEquipmentFromBag());
                    key = Console.ReadKey(true);
                    if (IsEscOrNonLetterKey(key.Key))
                        break;
                    stringBuilder.Append(_gameProcessor.Equip(key.KeyChar));
                    break;
                case ConsoleKey.D:
                    Console.WriteLine(Messages.HavePattern + Messages.MakeYourChoice,
                                      _gameProcessor.ShowEquipmentFromBag());
                    key = Console.ReadKey(true);
                    if (IsEscOrNonLetterKey(key.Key))
                        break;
                    stringBuilder.Append(_gameProcessor.Drop(key.KeyChar));
                    break;
                case ConsoleKey.E:
                    Console.WriteLine(Messages.WearPattern, _gameProcessor.ShowEquipped());
                    Console.WriteLine(Messages.PressToPattern, "letter", "equip/unequip");
                    key = Console.ReadKey(true);
                    if (IsEscOrNonLetterKey(key.Key))
                        break;
                    stringBuilder.Append(_gameProcessor.Unequip(key.KeyChar));
                    break;
                case ConsoleKey.Spacebar:
                    var items = _gameProcessor.ShowCurrentCellItems();
                    if (!items.Contains("b) "))
                    {
                        stringBuilder.Append(_gameProcessor.PickUp('a'));
                        break;
                    }
                    Console.WriteLine(Messages.SeePattern + Messages.MakeYourChoice, items);
                    Console.WriteLine(Messages.PressToPattern, "letter", "pick up");
                    key = Console.ReadKey(true);
                    if (IsEscOrNonLetterKey(key.Key))
                        break;
                    stringBuilder.Append(_gameProcessor.PickUp('*'));
                    break;

                    #region general

                case ConsoleKey.F1:
                    stringBuilder.Append(_gameProcessor.GetHelp());
                    break;
                case ConsoleKey.Q:
                    Console.WriteLine(Messages.Quit);
                    if (Console.ReadKey(true).Key == ConsoleKey.Y)
                        Environment.Exit(0);
                    break;

                    #endregion
            }
            return stringBuilder.ToString();
        }

        private static bool IsEscOrNonLetterKey(ConsoleKey key)
        {
            return key == ConsoleKey.Escape ||
                   (key != ConsoleKey.Multiply && (key < ConsoleKey.A || key > ConsoleKey.Z));
        }

        #endregion

        private static ConsoleColor SetColor(Colors color)
        {
            switch (color)
            {
                case Colors.White:
                    return ConsoleColor.White;
                case Colors.Black:
                    return ConsoleColor.Black;
                case Colors.Blue:
                    return ConsoleColor.Blue;
                case Colors.Cyan:
                    return ConsoleColor.Cyan;
                case Colors.DarkBlue:
                    return ConsoleColor.DarkBlue;
                case Colors.DarkCyan:
                    return ConsoleColor.DarkCyan;
                case Colors.DarkGray:
                    return ConsoleColor.DarkGray;
                case Colors.DarkGreen:
                    return ConsoleColor.DarkGreen;
                case Colors.DarkMagenta:
                    return ConsoleColor.DarkMagenta;
                case Colors.DarkRed:
                    return ConsoleColor.DarkRed;
                case Colors.DarkYellow:
                    return ConsoleColor.DarkYellow;
                case Colors.Gray:
                    return ConsoleColor.Gray;
                case Colors.Green:
                    return ConsoleColor.Green;
                case Colors.Magenta:
                    return ConsoleColor.Magenta;
                case Colors.Red:
                    return ConsoleColor.Red;
                case Colors.Yellow:
                    return ConsoleColor.Yellow;
                default:
                    throw new NotSupportedException(color.ToString());
            }
        }
    }
}
